package com.yueyang.sojourn.server.controller.project;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.dto.AddProjectDto;
import com.yueyang.sojourn.server.model.dto.EditProjectDto;
import com.yueyang.sojourn.server.model.enums.OperStatusEnum;
import com.yueyang.sojourn.server.model.vo.SojournProjectVo;
import com.yueyang.sojourn.server.service.sojourn.project.ReadProjectService;
import com.yueyang.sojourn.server.service.sojourn.project.WriteProjectService;
import com.yueyang.sojourn.server.validation.annotation.EnumValid;
import com.yueyang.uc.model.UcUserV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author yemingxing
 * @summary 旅居项目 Controller
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-02-12
 */
@RestController
@Slf4j
@Api(tags = "项目 API")
@RequestMapping("/sojourn/project")
public class SojournProjectController extends BaseController {

    @Autowired
    private ReadProjectService readProjectService;
    @Autowired
    private WriteProjectService writeProjectService;

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "新建项目")
    @PostMapping("/add")
    public Long addProject(@RequestBody @Validated AddProjectDto projectDto, BindingResult bindingResult) {

        FieldError fieldError = bindingResult.getFieldError();
        if (Objects.nonNull(fieldError)) {
            JAssert.justFailed(fieldError.getDefaultMessage());
        }

        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        return writeProjectService.addProject(loginUserId, projectDto);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "编辑项目")
    @PostMapping("/edit")
    public void editProject(@RequestBody @Validated EditProjectDto projectDto, BindingResult bindingResult) {

        FieldError fieldError = bindingResult.getFieldError();
        if (Objects.nonNull(fieldError)) {
            JAssert.justFailed(fieldError.getDefaultMessage());
        }

        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        writeProjectService.editProject(loginUserId, projectDto);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "修改状态")
    @PostMapping("/alterStatus")
    public void editOperStatus(@ApiParam(value = "项目id", required = true) @RequestParam Long projectId,
                               @ApiParam(value = "营业状态(枚举值)", required = true) @EnumValid(clazz = OperStatusEnum.class) @RequestParam Integer operStatus) {

        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        writeProjectService.editOperStatus(loginUserId, projectId, operStatus);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "项目列表分页查询")
    @GetMapping("/page")
    public PageInfo<SojournProjectVo> findPage(@ApiParam(value = "关键词") @RequestParam(required = false) String keyWord,
                                               @ApiParam(value = "营业状态(枚举值)") @RequestParam(required = false) Integer operStatus,
                                               @RequestParam int pageNo,
                                               @RequestParam int pageSize) {
        UcUserV1 ucUserV1 = super.getLoginUser();
        long loginUserId = ucUserV1.getId();
        return readProjectService.findPage(loginUserId, keyWord, operStatus, pageNo, pageSize);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "根据营业状态查询项目")
    @GetMapping("/findByOperStatus")
    public PageInfo<SojournProjectVo> findByOperStatus(@ApiParam(value = "营业状态(枚举值，多个以英文逗号\",\"间隔)", required = true) @RequestParam String operStatus,
                                                       @RequestParam(defaultValue = "1", required = false) int pageNo,
                                                       @RequestParam(defaultValue = "15", required = false) int pageSize) {
        return readProjectService.selectByOperStatus(operStatus, pageNo, pageSize);
    }

    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "根据项目id查询项目详情")
    @GetMapping("/{projectId}")
    public SojournProjectVo findById(@ApiParam(value = "项目id", required = true) @PathVariable long projectId) {
        return readProjectService.selectVoByPrimaryKey(projectId);
    }
}
